package gas.model;
 
import gas.view.*;
import java.util.*;

/***
 * GasStation is the data structure to hold gas station variables.
 * 
 * Attributes: 
 * currentPrice
 * freshness
 * location
 * popularity : holds an integer value for every hour of the day
 *              can also refer to crowdedness.
 * 
 */
public class GasStation {   private int currentPrice;
                            private int freshness;
                            private String location;
                            private int popularity[] = new int [24]; // wait time in minutes for each hour of day.
                            private int stationID;
                            private String stationName;
                            private Random tempRandom = new Random();
                          
  public void setStationName(String stationName) { this.stationName = stationName; }
  public String getStationName() { return stationName; }
  public void setCurrentPrice(int currentPrice) {
    if ( currentPrice < 0 ) return; // make sure price is not negative
    /*TODO: Insert notification to admin if it exceeds a threshold of
     *      tolerance for price variation.
     */
    this.currentPrice = currentPrice;
  }
  public int getCurrentPrice() { return currentPrice; }
  public void setFreshness(int freshness) { this.freshness = freshness; }
  public int getFreshness() { return freshness; }
  public void setLocation(LocationsEnum locEnum) { this.location = locEnum.location(); }
  public String getLocation() { return location; }
  public void setPopularity(int i, int value ) { this.popularity[i] = (value > 0) ? value : value * -1; }
  public int getPopularity( int i ) { return popularity[i]; }
  public void setStationID(int stationID) { this.stationID = stationID; }
  public int getStationID() { return stationID; }
  
  /* 
   * GasStation Constructor
   */
  public GasStation(){
    /*** Create Random popularity times... for kicks. ***/
    for(int i = 0; i < popularity.length; i++)
      setPopularity(i, (tempRandom.nextInt() % 11));
  }
  public GasStation(String sN, int cP, LocationsEnum LE){//String loc){
    setStationName(sN);
    setCurrentPrice(cP);
    setLocation(LE);
    
    for(int i = 0; i < popularity.length; i++)
      setPopularity(i, (tempRandom.nextInt() % 11));
  }
      
  //403 
  public String inDollars ( int centsTotal ){
      String tempString = "$";
      int dollars = centsTotal/100;
      int cents = centsTotal%100;
      
      if( cents < 10 )
          return tempString + dollars + ".0" + cents;
      else
          return tempString + dollars + "." + cents;
  }
  
  public String printDailyWaitTime(){
    StringBuffer retStringBuff = new StringBuffer("[");
    for (int i=0; i<popularity.length-1; i++){
      retStringBuff.append(popularity[i]+", ");
    }
    retStringBuff.append(popularity[popularity.length-1]+"]");
    return retStringBuff.toString();
  }
  
  public String toString(){
    String retString = "\nStation Name:  " + getStationName() +
                       "\nStation ID:    " + getStationID() +
                       "\nLocation:      " + getLocation() +
                       "\nWait Time:     " + printDailyWaitTime() +
                       "\nCurrent Price: " + inDollars(getCurrentPrice());
    return retString;
  }
  
  public static boolean selfTest(){
    boolean passAll = true;
    GasStation testGS = new GasStation();
    
    testGS.setStationID(565);
    testGS.setStationName("Shell");
    testGS.setLocation(LocationsEnum.SANDIEGO);
    
    passAll = passAll & "Shell".equals(testGS.getStationName());
    System.out.println(testGS.toString());
    
    return passAll;
  }
  public static void main(String[] args){
    selfTest();
  }
  
}
